﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SiGeSIB.Data.Common;
using SiGeSIB.Data.Management;

namespace SiGeSIB.Business.Management
{
    public class ContributorValidator
    {
        public static ExceptionsContainer ValidateContributor(Contributor contributor)
        {
			ExceptionsContainer ex = null;

			switch (contributor.Type)
			{
				case EContributorType.Member:
					ex = ValidateMember(contributor as Member);
					break;
				case EContributorType.Visitor:
					break;
				case EContributorType.Away:
					break;
				case EContributorType.Contributor:
					break;
				default:
					break;
			}

			if (ex == null)
				ex = new ExceptionsContainer();

            if (string.IsNullOrEmpty(contributor.Name))
                ex.Add(new ValidationError("InvalidName", "O nome é um campo obrigatório."));
            if 
            (
                contributor.DocumentType.HasValue && contributor.DocumentType != EDocumentType.Unknown
                &&
                (
                    string.IsNullOrEmpty(contributor.DocumentNumber)
                    ||
                    !CommonValidator.ValidateDocument(contributor.DocumentType.Value, contributor.DocumentNumber)
                )
            )
                ex.Add(new ValidationError("InvalidDocumentNumber", "Número de documento inválido."));

            return ex;
        }

        public static ExceptionsContainer ValidateMember(Member member)
        {
			ExceptionsContainer ex = new ExceptionsContainer();

			if (member.BirthdayDate.HasValue && member.MembershipDate.CompareTo(member.BirthdayDate.Value) < 0)
                ex.Add(new ValidationError("InvalidMembershipDate", "A data de membresia não pode ser anterior à data de nascimento."));
            if (member.DisruptionDate.HasValue && member.MembershipDate.CompareTo(member.DisruptionDate) > 0)
                ex.Add(new ValidationError("InvalidMembershipDate", "A data de afastamento não pode ser posterior à data de membresia."));

            return ex;
        }
    }
}
